from django.conf.urls import url
from rest_framework.mixins import ListModelMixin, CreateModelMixin
from rest_framework.viewsets import GenericViewSet
from rest_framework.decorators import action


from AnalysisService import settings
from AnalysisService.util.DAMathUtil import PhoneNumberUtil
from AnalysisService.util.DAResultJson import DAResultNoraml, DAResultEntry, DAResultCommon
from .entry_models import AYEntryModel
from .entry_serializers import AYEntrySerializer, AYEntryActionSerializer
from AnalysisApp.analysis_db.entry.AYEntryDB import EntryDB


class AYEntryQueryView(ListModelMixin, GenericViewSet):
    queryset = AYEntryModel.objects.all()
    serializer_class = AYEntrySerializer

    @action(methods=['post'], detail=True)
    def queryEntry(self, request):
        """
            获取 报名列表 name
        """
        data = request.data

        name = ''
        lenght = len(data)

        if lenght == 1:
            name = data['name']

        search = {
            '$or': [
                {'name': {'$regex': name}},
                {'phonenumber': {'$regex': name}}
            ]}

        db = EntryDB()
        entrys = db.queryByTime(search)

        # queryset = self.queryset
        # entrys = queryset.filter(**search).order_by("-createtime")

        page = self.paginate_queryset(entrys)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = AYEntrySerializer(entrys, many=True)
        dic = serializer.data

        return DAResultNoraml.successWithData(dic)

    @staticmethod
    def url_array():
        return url(r'^entry-list/', AYEntryQueryView.as_view({'get': 'list', 'post': 'queryEntry'}),
                   name='entry-list')


class AYEntryActionView(ListModelMixin, GenericViewSet):
    queryset = AYEntryModel.objects.all()
    serializer_class = AYEntryActionSerializer

    @action(methods=['post'], detail=True)
    def addEntry(self, request):
        """
            添加 报名数据 name,phonenumber,source(来源 wap/pc),url
        """
        data = request.data
        lenght = len(data)

        name = ''
        phonenumber = ''
        source = ''
        url = ''
        remark1 = ''
        remark2 = ''

        if lenght == 4:

            name = data['name']
            phonenumber = data['phonenumber']
            source = data['source']
            url = data['url']

        elif lenght == 6:
            name = data['name']
            phonenumber = data['phonenumber']
            source = data['source']
            url = data['url']
            remark1 = data['remark1']
            remark2 = data['remark2']

        isCheck = PhoneNumberUtil.check(phonenumber)

        if not isCheck:
            return DAResultCommon.phoneNumberFail()

        db = EntryDB()

        if db.isExist(phonenumber, url):
            return DAResultEntry.entryError()

        isbool = db.add_one(name, phonenumber, source, url, remark1, remark2)

        if isbool:
            return DAResultNoraml.success()

        return DAResultNoraml.error()

    @staticmethod
    def url_array():
        return url(r'^entry-action/', AYEntryActionView.as_view({'get': 'list', 'post': 'addEntry'}),
                   name='entry-action')
